home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / zoo21src.zoo / atari.c < prev    next >
C/C++ Source or Header  |  1991-07-24  |  7KB  |  257 lines

  1. #include <osbind.h>
  2. #include <stdlib.h>
  3. #include <types.h>
  4. #include <stat.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include "errors.i"
  8. #include "zoofns.h"
  9.  
  10. #ifdef __GNUC__
  11. size_t __DEFAULT_BUFSIZ__ = 32768L;
  12. long _stksize = 16384L;
  13. #endif
  14.  
  15. #ifdef GLOB
  16. static void add_targv(char *s);
  17. extern char **glob(char *, int);
  18. extern int contains_wild(char *);
  19.  
  20. char **targv = (char **)NULL;
  21. int  targc = 0;
  22. #endif
  23.  
  24. /****************
  25. Function fixfname() converts the supplied filename to a syntax
  26. legal for the host system.  It is used during extraction.
  27. */
  28.  
  29. char *fixfname(fname)
  30. char *fname;
  31. {
  32.         /* note this is valid for gcc lib only, where the lib does file name
  33.          * conversions (see LIBSRC/unx2dos.c)
  34.          */
  35.         return (fname); /* default is no-op */
  36. }
  37.  
  38. /* No file truncate call for the Atari ST.  Make it a no-op. */
  39. int zootrunc(f)
  40. FILE *f;
  41. {
  42.     return 0;
  43. }
  44.  
  45. /*
  46. Function gettz(), returns the offset from GMT in seconds of the
  47. local time, taking into account daylight savings time */
  48.  
  49. #ifdef GETTZ
  50. long gettz()
  51. {
  52. #ifdef __GNUC__
  53.     struct tm *tm;
  54.     static char first_time = 1;
  55.     static long t;
  56.     extern time_t _timezone;
  57.  
  58.     if(first_time)
  59.     {
  60.         first_time = 0;
  61.         t = time ((long *) 0);
  62.         tm = localtime (&t);
  63.         t = _timezone - tm->tm_isdst*3600;
  64.     }
  65.     return t;
  66. #else
  67.     return 0L;
  68. #endif
  69. }
  70. #endif /* GETTZ */
  71.  
  72. /* Function isadir() returns 1 if the supplied handle is a directory, 
  73. else it returns 0.  
  74. */
  75.  
  76. int isadir (file)
  77. ZOOFILE file;
  78. {
  79.    int handle = fileno(file);
  80.    struct stat buf;           /* buffer to hold file information */
  81.    if (fstat (handle, &buf) == -1) {
  82.       return (0);             /* inaccessible -- assume not dir */
  83.    } else {
  84.       return (buf.st_mode & S_IFMT) == S_IFDIR;
  85.    }
  86. }
  87.  
  88. #ifdef SPECEXIT
  89. /*
  90.  * detect shells
  91.  */
  92. #include <sysvars.h>
  93. #include <support.h>
  94.  
  95. static  long    (*shell_p)(char *);
  96. static int _is_a_shell(void)
  97. {
  98.     int ret;
  99.     
  100.     shell_p = (long (*)(char *)) get_sysvar((void *) _shell_p);
  101.     if(!(long)shell_p) return(0);
  102.     ret = 1;
  103.     if(((*((long *)(((long) shell_p)-10))) & 0x00FFFFFFL) == 0x00420135L)
  104.         ret = 2;                                     /* Gulam */
  105.     if(*((long *)(((long) shell_p)-4)) == 0xCDCEC5D2L || /* Master 5.0 */
  106.        *((long *)(((long) shell_p)-8)) == 0x4D415354L)   /* Master 5.4 */
  107.         ret = 3;                                     /* Master */
  108.     if(*((long *)(((long) shell_p)-4)) == 0x21534821L)   /* Craft */
  109.         ret = 4;
  110.     return(ret);
  111. }
  112.  
  113. void zooexit(status)
  114. int status;
  115. {
  116.     extern char *getenv(const char *);
  117.     char *p;
  118.     char dk = 0;
  119.  
  120.     if(!getenv("SHELL"))
  121.     { /* no env SHELL */
  122.         if(!(p = getenv("ZOOPAUSE")))
  123.         { /* no env ZOOPAUSE */
  124.             if(!_is_a_shell())
  125.                 dk = 1; /* not called from a reasonable shell */
  126.         } else if((*p == 'y') || (*p == 'Y'))
  127.             dk = 1; /* ZOOPAUSE is 'y' or 'Y' */
  128.     }
  129.     if(dk)
  130.     {
  131.         fprintf(stderr,"\nHit any key to exit ......");
  132.         fflush(stderr);
  133.         (void)Bconin(2);
  134.     }
  135.     exit(status);
  136. }
  137. #endif /* SPECEXIT */
  138.  
  139. #include <types.h>
  140. #include <stat.h>
  141. static int nameisdir(n)
  142. char *n;
  143. {
  144.     struct stat s;
  145.  
  146.     if(stat(n, &s))
  147.         return 0;
  148.     return ((s.st_mode) & S_IFMT) == S_IFDIR;
  149. }
  150. #ifdef GLOB
  151. int main (argc, argv)
  152. int argc;
  153. char **argv;
  154. {
  155.     int i;
  156.     char **matches;
  157.     int decend_dir;             /* decend subdirs recursively when adding? */
  158.     void free_all(void);
  159.     int  do_globbing = 0;
  160.     
  161.     if (argv[0][0] == '\0')     /* Add program name if we're called */
  162.         add_targv("zoo");       /* from the desktop */ 
  163.     else
  164.         add_targv(argv[0]);     /* Else use what the shell sent */
  165.  
  166.     if (argc < 2)       /* If there are no commands, let zoo handle it */
  167.         goto no_glob;
  168.  
  169.     /* Now some hacks for the novice commands */
  170.     if (str_icmp(argv[1], "-backup") == 0)
  171.         strcpy(argv[1], "ah//");
  172.     else if (str_icmp(argv[1], "-restore") == 0)
  173.         strcpy(argv[1], "x//");
  174.  
  175.     add_targv(argv[1]);         /* Add the commands */
  176.  
  177.     if (argc < 3)               /* If there's no archive name, let zoo */
  178.         goto no_glob;           /* handle it */
  179.     add_targv(argv[2]);         /* Add the archive name */
  180.  
  181.     if (argc < 4)               /* If there are no filespecs, we don't */
  182.         goto no_glob;           /* have to glob */
  183.  
  184.     /* decide is we will do any globbing (only if its an 'a' cmd) */
  185.     if (!(
  186.       (*argv[1] != 'a') && (*argv[1] != 'A') &&
  187.       (str_icmp(argv[1], "-add") != 0) &&
  188.       (str_icmp(argv[1], "-freshen") != 0) &&
  189.       (str_icmp(argv[1], "-update") != 0) &&
  190.       (str_icmp(argv[1], "-move") != 0)))
  191.     do_globbing = 1;
  192.     
  193.     /* Is '//' one of the options given for the `a' command ? */
  194.     decend_dir = ((*argv[1] == 'a') || (*argv[1] == 'A')) &&
  195.              (strstr(argv[1], "//") != NULL);
  196.  
  197.     /* Now we are going to start adding filenames that we want to send */
  198.     /* to Zoo */ 
  199.     for (i = 3; i < argc; i++)
  200.     {
  201.     /* Does it have any wildcards ? */ 
  202.     if (do_globbing && (contains_wild(argv[i]) ||
  203.                 (decend_dir && nameisdir(argv[i]))))
  204.                 if ((matches = glob(strlwr(argv[i]), decend_dir)) != NULL)
  205.                 {
  206.                         char **m = matches;
  207.                         while(*matches)
  208.                                 add_targv(*matches++);
  209.                         free(m);
  210.                 }
  211.                 else 
  212.                     prterror('e', no_match); /* Not FATAL because */
  213.                                              /* there could have been */
  214.                                              /* some valid files that */
  215.                                              /* didn't contain wildcards */
  216.                                              /* This will probably */
  217.                                              /* change  -- bjsjr */
  218.         else
  219.             add_targv(argv[i]); /* Add any filename that doesn't */
  220.                                 /* contain wildcards */
  221.     }
  222.     
  223. no_glob:    
  224.     add_targv("");      /* yes, *nix compatibility */
  225.     targv[--targc] = NULL;
  226.  
  227.     return zoomain(targc, targv);
  228. }
  229.  
  230. #define CHUNK_SIZE 16
  231.  
  232. static void add_targv(char *s)
  233. {
  234.     static size_t allocsize = 0;
  235.     static int avail = 0;
  236.     
  237.     if(avail == 0)
  238.     {  /* need more mem */
  239.         allocsize += (CHUNK_SIZE * sizeof(char **));
  240.         avail = CHUNK_SIZE;
  241.         targv = (char **)((targv == (char **)NULL) ? malloc(allocsize)
  242.                           : realloc(targv, allocsize));
  243.         if(!targv)
  244.             prterror('f', no_memory);
  245.     }
  246.  
  247.     targv[targc++] = s;
  248.     avail--;
  249. }
  250. #endif /* GLOB */
  251.  
  252. /* Standard UNIX-compatible time functions */
  253. #include "nixtime.i"
  254.  
  255. /* Standard UNIX-specific file attribute routines */
  256. #include "nixmode.i"
  257.